﻿using Gym.Application.Common.Interfaces;
using Gym.Domain.GymAggregate.Events;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gym.Application.Sessions.Events
{
    public class RoomRemovedEventHandler : INotificationHandler<RoomRemovedEvent>
    {
        private readonly ISessionsRepository _sessionsRepository;

        public RoomRemovedEventHandler(ISessionsRepository sessionsRepository)
        {
            _sessionsRepository = sessionsRepository;
        }

        public async Task Handle(RoomRemovedEvent notification, CancellationToken cancellationToken)
        {
            var sessions = await _sessionsRepository.ListByRoomIdAsync(notification.Room.Id);

            sessions.ForEach(session =>
            {
                session.Cancel();
            });

            await _sessionsRepository.RemoveRangeAsync(sessions);
        }
    }
}
